package com.wingscode.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wingscode.common.annotation.SysLog;
import com.wingscode.common.utils.PageUtils;
import com.wingscode.common.utils.R;
import com.wingscode.common.validator.ValidatorUtils;
import com.wingscode.common.validator.group.AddGroup;
import com.wingscode.common.validator.group.UpdateGroup;
import com.wingscode.modules.sys.entity.BuildQualEntity;
import com.wingscode.modules.sys.entity.BuildQualSubEntity;
import com.wingscode.modules.sys.service.BuildQualService;

import com.wingscode.modules.sys.service.BuildQualSubService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 资质
 *
 * @author yan
 */
@RestController
@RequestMapping("/buildQual")
public class BuildQualController extends AbstractController {
	@Autowired
	private BuildQualService buildQualService;
	@Autowired
	private BuildQualSubService buildQualSubService;

	
	/**
	 * 资质
	 */
	@GetMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		PageUtils page = buildQualService.queryPage(params);

		return R.ok().put("page", page);
	}
	
	/**
	 * 资质详情
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		BuildQualEntity buildQualEntity = buildQualService.getById(id);
		if (buildQualEntity != null) {
			List<BuildQualSubEntity> subList1 = buildQualSubService.list(
					new QueryWrapper<BuildQualSubEntity>().eq("build_qual_id", id)
							.eq("type", 1)
			);
			buildQualEntity.setArchitectlSubList(subList1);

			List<BuildQualSubEntity> subList2 = buildQualSubService.list(
					new QueryWrapper<BuildQualSubEntity>().eq("build_qual_id", id)
							.eq("type", 2)
			);
			buildQualEntity.setJobSubList(subList2);
		}

        return R.ok().put("buildQual", buildQualEntity);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存资质")
	@PostMapping("/save")
	public R save(@RequestBody BuildQualEntity entity){
		entity.setCreateTime(new Date());
		ValidatorUtils.validateEntity(entity, AddGroup.class);

		buildQualService.save(entity);

		// 保存建造师专业明细
		if (entity.getArchitectlSubIdList() != null && entity.getArchitectlSubIdList().size() > 0) {
			for (Integer id : entity.getArchitectlSubIdList()) {
				BuildQualSubEntity subEntity = new BuildQualSubEntity();

				subEntity.setBuildQualId(entity.getId());
				subEntity.setDicId(id);
				subEntity.setType(1);

				buildQualSubService.save(subEntity);
			}
		}

		// 保存职称人员专业明细
		if (entity.getJobSubIdList() != null && entity.getJobSubIdList().size() > 0) {
			for (Integer id : entity.getJobSubIdList()) {
				BuildQualSubEntity subEntity = new BuildQualSubEntity();

				subEntity.setBuildQualId(entity.getId());
				subEntity.setDicId(id);
				subEntity.setType(2);

				buildQualSubService.save(subEntity);
			}
		}

		return R.ok();
	}

	/**
	 * 修改资质
	 */
	@SysLog("修改资质")
	@PostMapping("/update")
	public R update(@RequestBody BuildQualEntity entity){
		entity.setUpdateTime(new Date());
		ValidatorUtils.validateEntity(entity, UpdateGroup.class);

		buildQualService.updateById(entity);

		// 保存建造师专业明细
		if (entity.getArchitectlSubIdList() != null && entity.getArchitectlSubIdList().size() > 0) {
			// 删除原有建造师专业明细
			buildQualSubService.remove(new QueryWrapper<BuildQualSubEntity>()
					.eq("build_qual_id", entity.getId())
					.eq("type", 1)
			);
			// 保存现有建造师专业明细
			for (Integer id : entity.getArchitectlSubIdList()) {
				BuildQualSubEntity subEntity = new BuildQualSubEntity();

				subEntity.setBuildQualId(entity.getId());
				subEntity.setDicId(id);
				subEntity.setType(1);

				buildQualSubService.save(subEntity);
			}
		}

		// 保存职称人员专业明细
		if (entity.getJobSubIdList() != null && entity.getJobSubIdList().size() > 0) {
			// 删除原有职称人员专业明细
			buildQualSubService.remove(new QueryWrapper<BuildQualSubEntity>()
					.eq("build_qual_id", entity.getId())
					.eq("type", 2)
			);
			// 保存现有职称人员专业明细
			for (Integer id : entity.getJobSubIdList()) {
				BuildQualSubEntity subEntity = new BuildQualSubEntity();

				subEntity.setBuildQualId(entity.getId());
				subEntity.setDicId(id);
				subEntity.setType(2);

				buildQualSubService.save(subEntity);
			}
		}

		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@SysLog("删除资质")
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] Ids){
		buildQualService.delete(Ids);

		return R.ok();
	}

	/**
	 * 根据资质id、等级获得资质详情
	 */
	@GetMapping("/getByGrade")
	public R getByGrade(@RequestParam Integer nameId, @RequestParam Integer grade) {
		if (nameId == null)
			return R.error("nameId为空");
		if (grade == null)
			return R.error("grade为空");

		BuildQualEntity entity = buildQualService.getByGrade(nameId, grade);
		if (entity != null) {
			List<BuildQualSubEntity> subList1 = buildQualSubService.getList(entity.getId(), 1);
			entity.setArchitectlSubList(subList1);

			List<BuildQualSubEntity> subList2 = buildQualSubService.getList(entity.getId(), 2);
			entity.setJobSubList(subList2);
		}

		return R.ok(200, entity);
	}

	/**
	 * 获得资质list
	 */
	@GetMapping("/getBuildQual")
	public R getBuildQual(@RequestParam Integer nameId, @RequestParam Integer grade, @RequestParam Integer qualType) {
		List<BuildQualEntity> list = buildQualService.getBuildQual(nameId, grade, qualType);

		return R.ok(200, list);
	}
}
